////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2012 www.iflytek.com. All Rights Reserved.
//  This software for customer relationship management system, developed by Noo team.
//  Software code and design for the team, copy rights reserved.
////////////////////////////////////////////////////////////////////////////////

package com.iflytek.ci.cums.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.iflytek.ci.common.utils.excel.Excels;
import com.iflytek.ci.cums.dao.AccountDao;
import com.iflytek.ci.cums.dao.UserBatchDao;
import com.iflytek.ci.cums.dao.UserRoleDao;
import com.iflytek.ci.cums.domain.Account;
import com.iflytek.ci.cums.service.AccountService;
import com.iflytek.ci.cums.util.AccountExcelParser;
import com.iflytek.framework.pagination.PaginationCriteria;
import com.iflytek.framework.pagination.PaginationResult;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 帐号服务接口实现.
 * </p>
 * 
 * @author poplar.yfyang
 * @version 1.0 2012-10-25 10:00 PM
 * @since JDK 1.5
 */

@Service("accountService")
@Transactional(readOnly = true)
public class AccountServiceImpl implements AccountService {
	/** 日志 */
	private static final Logger LOGGER = LoggerFactory
			.getLogger(AccountService.class);
	/** 帐号DAO */
	@Autowired
	private AccountDao accountDao;
	/** 帐号相关批处理DAO */
	@Autowired
	private UserBatchDao userBatchDao;
	@Autowired
	private UserRoleDao userRoleDao;

	@Override
	public Account accountWithName(String name) {
		return accountDao.fromName(name);
	}

	@Transactional
	@Override
	public long addAccount(Account account) {
		final Date now = new Date();
		account.setUpdated(now);
		account.setCreate(now);
		int id = accountDao.addUser(account);
		userRoleDao.addRoles(accountDao.fromName(account.getLogin()).getId(),
				account.getRoles());
		return id;
	}

	@Override
	public PaginationResult<Account> findAccounts(PaginationCriteria criteria) {
		return accountDao.pageUsers(criteria);
	}

	@Transactional
	@Override
	public void deleteAccount(String[] id_array) {
		accountDao.removeAccounts(id_array);
		for (String s : id_array) {
			userRoleDao.deleteRole(s);
		}
	}

	@Transactional
	@Override
	public void updateAccounts(Account account) {
		account.setUpdated(new Date());
		accountDao.updateAccounts(account);
		if (account.getRoles() != null) {
			long id = accountDao.fromName(account.getLogin()).getId();
			userRoleDao.deleteRole(String.valueOf(id));
			userRoleDao.addRoles(id, account.getRoles());
		}
	}

	@Override
	public Account fromId(long id) {
		Account account = accountDao.fromId(id);
		account.setRoles(userRoleDao.get_roles(account.getId()));
		return account;
	}

	@Override
	public PaginationResult<Account> fuzzy_search(PaginationCriteria criteria,
			String search) {
		return accountDao.fuzzy_search_page_account(search, criteria);
	}

	@Override
	public List<Account> get_all() {
		return accountDao.get_all();
	}

	@Override
	public List<Account> get_name(String[] ids) {
		return accountDao.get_name(ids);
	}

	@Override
	public List<Account> allLeaders() {
		return accountDao.allLeaders();
	}

	@Override
	public void chpwd(Account account) {
		accountDao.chpwd(account);
	}

	@Transactional
	@Override
	public void import_account(MultipartFile upload_file) {
		File file = null;
		// 文件临时目录存放
		try {
			String temp_file_path = System.getProperty("java.io.tmpdir")
					+ File.separator + upload_file.getOriginalFilename();
			file = new File(temp_file_path);
			FileUtils.writeByteArrayToFile(file, upload_file.getBytes());
			List<Account> list = (List<Account>) Excels.readExcel(0,
					new AccountExcelParser(), temp_file_path);
			userBatchDao.batch_account(list);
		} catch (IOException e) {
			LOGGER.error("导入用户数据时文件上传出现异常!", e);
		} finally {
			FileUtils.deleteQuietly(file);
		}

	}

	@Override
	public List<String> get_names(String[] login) {
		return accountDao.get_names(login);
	}

	@Transactional(isolation = Isolation.READ_UNCOMMITTED)
	@Override
	public void update_time(Account account) {
		account.setUpdated(new Date());
		accountDao.updateAccounts(account);
	}
}
